<?php
class UserGroup
{
	private $db;
	
	function __construct() {
		$this->db = godhouse::app()->db;
	}
	
	public function find($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->fetch_first("SELECT * FROM god_user_group $condition");
	}
	
	public function findByPk($pk) {
		return $this->db->fetch_first("SELECT * FROM god_user_group WHERE groupId='$pk'");
	}
	
	public function findAll($condition='', $page=0, $ppp=GODHOUSE_PPP3, $order='') {
		if($condition) 
			$condition = "WHERE $condition";
		if($order) {
			$condition .= "ORDER BY $order";
		} else 
			$condition .= "ORDER BY groupId DESC";
		
		$sqladd = '';
		if($page && $ppp) {
			$start = ($page-1)*$ppp;
			$sqladd = "LIMIT $start, $ppp";
		}
		return $this->db->fetch_all("SELECT * FROM god_user_group $condition $sqladd");
	}
	
	public function findAllByPk($pk) {
		return $this->db->fetch_all("SELECT * FROM god_user_group WHERE groupId='$pk'");
	}
	
	public function update($condition, $att=array()){
		return $this->db->update('god_user_group', $att, $condition);
	}
	
	public function updateByPk($pk, $att=array()) {
		$condition = "groupId='$pk'";
		return $this->db->update('god_user_group', $att, $condition);
	}
	
	public function insert($att=array()){
		return $this->db->insert('god_user_group', $att);
	}
	
	public function delete($condition) {
		$this->db->query("DELETE FROM god_user_group WHERE $condition");
	}
	
	public function deleteByPk($pk) {
		$condition = "groupId='$pk'";
		$this->db->query("DELETE FROM god_user_group WHERE $condition");
	}
	
	public function count($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->result_first("SELECT count(*) FROM god_user_group $condition");
	}
}
?>